//
// Created by madison on 2022/7/28.
//

#include "vector"

using namespace std;

class Solution {
public:
    // 方法一：动态规划
    int rob(vector<int> &nums) {
        if (nums.empty()) {
            return 0;
        }
        int size = nums.size();
        if (size == 1) {
            return nums[0];
        }
        vector<int> dp = vector<int>(size, 0);
        dp[0] = nums[0];
        dp[1] = max(nums[0], nums[1]);
        for (int i = 2; i < size; ++i) {
            dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]);
        }
        return dp[size - 1];
    }

    int rob1(vector<int> &nums) {
        if (nums.empty()) {
            return 0;
        }
        int size = nums.size();
        if (size == 1) {
            return nums[0];
        }
        int first = nums[0], second = max(nums[0], nums[1]);
        for (int i = 2; i < size; ++i) {
            int temp = second;
            second = max(first + nums[i], second);
            first = temp;
        }
        return second;
    }
};

int main() {
    vector<int> nums = {2, 7, 9, 3, 1};
    Solution solution;
    printf("%d", solution.rob(nums));
    return 0;
}